{% extends "page_frame.html" %}
{% load static %}

{% block brand_title %}
    登录
{% endblock %}

{% block content %}
    <form action="#" class="form-horizontal m-auto" style="max-width: 500px">
        {% csrf_token %}

        <div class="mb-3 row">
            <label for="inputUser" class="col-3 col-form-label" >用户名：</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="inputUser" value="test1">
            </div>
        </div>

        <div class="mb-3 row">
            <label for="inputPassword" class="col-3 col-form-label">密码：</label>
            <div class="col-sm-10">
                <input type="password" class="form-control" id="inputPassword" value="yejue@123">
            </div>
        </div>
        <div class="mb-3">
            <button class="btn btn-primary" type="button" id="login">登录</button>
        </div>
    </form>
{% endblock %}

{% block script %}
    <script>
        var $is_username = false;
        var $is_password = false;

        {#获取用户名和密码#}
        let $username_input = $("#inputUser");
        let $password_input = $("#inputPassword");

        function getUsernamePwd(){
            let username = $username_input.val();
            let password = $password_input.val();

            return {username: username, password: password}
        }


        {#登录请求部分#}
        let $submit_button = $("#login");
        $submit_button.click(login);

        {#发送登录请求#}
        function login() {
            $
            .ajax({
                type: "POST",
                url: "/verify/5a181dd0aced496d9253c2cce29d6b80/",
                dataType: "JSON",
                data: getUsernamePwd(),
            })
            .done(function (res) {
                if (res.code != "0"){
                    alert(res.msg);
                }else{
                    alert("登录成功, 即将跳转");

                    setTimeout(function () {
                        if (!document.referer | document.referer === "/verify/login"){
                            window.location.href = '/home/'
                        }
                        else{
                            window.location.href = document.referrer
                        }
                    })
                }
            })
            .fail(function () {
                alert("网络迷路中");
            })
        }
    </script>
{% endblock %}

